import React, {FC, useState} from "react";
import styled from 'styled-components';
import {useNavigate} from "react-router-dom";
import {e} from "unocss";

// 使用大写字母命名 styled 组件
const LoginGroup = styled.form`
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    position: absolute;
    transition: 0.5s;
    box-sizing: border-box;
    padding: 20px;
`;

const InputField = styled.input`
    width: 100%;
    padding: 10px 5px;
    margin: 10px 0;
    border-top: 0;
    border-left: 2px solid #57AAB4;
    border-right: 0;
    border-bottom: 2px solid #57AAB4;
    outline: none;
    background: transparent;
    color: rgb(234, 234, 235);
    font-size: 15px;
    transition: 0.5s;

    &:focus {
        border-left: 2px solid transparent;
        border-bottom: 2px solid transparent;
        animation: animINP 5s linear infinite, animBTN 5s linear infinite;
    }
`;
const SubmitInputField=styled.input`
        width: 85%;
        padding: 10px 30px;
        cursor: pointer;
        display: block;
        margin: 30px auto 0 auto;
        background: linear-gradient(to right, #03a9f4,#57AAB4,#03a9f4);
        border:0;
        outline: none;
        border-radius: 30px;
        position: relative;
        z-index: 5;
        box-sizing: border-box;
        color:#fff;
        font-weight: bold;
        font-size:15px;
        transition: 0.5s;
    &:hover {
        background:#57AAB4;
        color: #fff;
        border-radius: 30px;
        box-shadow: 0 0 5px #57AAB4,
        0 0 25px #57AAB4,
        0 0 50px #57AAB4,
        0 0 100px #03e9f4;
    }
`
const LoginWarp: FC = () => {
    const navigate = useNavigate();
    const [userInfo,setUserInfo]=useState<{username:string,password:string}>({
        username:'',
        password:''
    })
    const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        const { name, value } = e.target;
        setUserInfo((prev) => ({
            ...prev,
            [name]: value,
        }));
    };

    const handleLogin = (e:React.FormEvent) => {
        e.preventDefault()
        if (userInfo.username === "zhangsan" && userInfo.password == "111111"){
            const date=Date.now()
            localStorage.setItem("access_token",date.toString())
            navigate("/")
        }else {
            alert("用户名密码错误")
        }
    }
    return (
        <LoginGroup id="login" onSubmit={handleLogin}>
            <InputField
                type="text"
                placeholder="请输入用户名/邮箱"
                value={userInfo?.username}
                name='username'
                onChange={handleInputChange}
                required
            />
            <InputField
                id="pwd"
                type="password"
                placeholder="请输入密码"
                value={userInfo?.password}
                name='password'
                onChange={handleInputChange}
                required
            />
            <SubmitInputField value="登录" type='submit'/>
        </LoginGroup>
    );
};

export default LoginWarp;
